using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace TmxUtil
{
    public class ResXItemCollection : Collection<ResXItem>
    {
        public ResXItemCollection()
        {
        }

        public ResXItemCollection(IEnumerable<ResX> resXList)
        {
            AddRange(resXList);
        }

        public void AddRange(ResX resX)
        {
            if (resX.Exists)
            {
                ResXStrings strings = resX.Strings;
                foreach (string localKey in strings.Keys)
                {
                    ResXItem item = new ResXItem(resX, localKey, strings[localKey]);
                    Add(item);
                }
            }
        }

        public void AddRange(IEnumerable<ResX> resXList)
        {
            foreach(ResX rx in resXList)
                AddRange(rx);
        }

        public string this[Project project, string resKey, string cultureName, string localKey]
        {
            get
            {
                ResXItem item = CreateOrFind(project, resKey, cultureName, localKey);
                return item.Value;
            }
            set
            {
                ResXItem item = CreateOrFind(project, resKey, cultureName, localKey);
                item.Value = value;
                item.ResX.Strings.Set(localKey, value);
            }
        }

        ResXItem CreateOrFind(Project project, string resKey, string cultureName, string localKey)
        {
            foreach (ResXItem item in this)
            {
                if (item.ResX.Project.File == project.File && item.ResX.Key == resKey && item.ResX.CultureName == cultureName && item.Key == localKey)
                    return item;
            }

            ResX resX = project.CreateOrFind(resKey, cultureName);
            ResXItem newItem = new ResXItem(resX, localKey, null);
            resX.Strings.Set(localKey, newItem.Value);
            this.Add(newItem);
            return newItem;
        }
    }
}
